/*
  Created by 1271107612@qq.com.
  User: DELL
  Date: 2022/4/11
  Time: 15:58
  DESCRIPTION: 
*/
package com.zzy.face.controller;


import com.zzy.face.dto.TeacherInfo;
import com.zzy.face.model.Account;
import com.zzy.face.service.AccountService;
import com.zzy.face.service.StudentService;
import com.zzy.face.service.TeacherService;
import com.zzy.face.utils.Result;
import com.zzy.face.utils.ResultCode;
import com.zzy.face.utils.jwt.Jwt;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping(value = "/teacher")
public class TeacherController {
    final Jwt jwt;
    final AccountService accountService;
    final TeacherService teacherService;
    final StudentService studentService;

    public TeacherController(Jwt jwt, AccountService accountService, TeacherService teacherService, StudentService studentService) {
        this.jwt = jwt;
        this.accountService = accountService;
        this.teacherService = teacherService;
        this.studentService = studentService;
    }

    @RequestMapping(value = "", method = RequestMethod.GET)
    @ApiOperation(value = "查看个人信息", notes = "查看个人信息")
    public Result get(HttpServletRequest request){
        try {
            TeacherInfo teacherInfo = teacherService.findTeacherInfo(jwt.getId(request));
            return new Result(ResultCode.SUCCESS, teacherInfo);
        } catch (Exception e) {
            return new Result(ResultCode.SERVER_ERROR, e);
        }
    }


    @RequestMapping(value = "/students", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部学生", notes = "获取全部学生")
    public Result studentInfos(HttpServletRequest request) {
        return new Result(ResultCode.SUCCESS,studentService.findStudentInfosInTeacher(jwt.getId(request)));
    }

    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ApiOperation(value = "修改个人信息", notes = "修改个人信息")
    public Result changeInfo(@RequestBody TeacherInfo form, HttpServletRequest request){
        int identity = Integer.parseInt(jwt.token(request).get("identity").toString());
        if ((identity>=accountService.findByMobile(form.getMobile()).getIdentity())){
            Account account = accountService.findByUid(form.getUid());
            account.setMobile(form.getMobile());
            account.setName(form.getName());
            account.setSex(form.getSex());
            account.setState(form.getState());
            account.setCollege(form.getCollege());
            accountService.save(account);
            return new Result(ResultCode.SUCCESS);
        }
        return new Result(ResultCode.UN_AUTHORISE);
    }

}
